﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.Entities.Providers
{
    [Serializable]
    public class PartitionOutput:ResultBase
    {
        #region props
        private int _PartitionCount;
        /// <summary></summary>
        public int PartitionCount
        {
            get
            {
                return this._PartitionCount;
            }
            set
            {
                this._PartitionCount = value;
            }
        }

        private int _TotalEntityCount;
        /// <summary></summary>
        public int TotalEntityCount
        {
            get
            {
                return this._TotalEntityCount;
            }
            set
            {
                this._TotalEntityCount = value;
            }
        }

        private string _PartitionResultFilePath;
        /// <summary></summary>
        public string PartitionResultFilePath
        {
            get
            {
                return this._PartitionResultFilePath;
            }
            set
            {
                this._PartitionResultFilePath = value;
            }
        }
        #endregion

        #region ctor
        public PartitionOutput():base(Guid.Empty,Guid.Empty)
        {
            this._PartitionCount = 0;
            this._TotalEntityCount = 0;
            this._PartitionResultFilePath = string.Empty;
        }

        public PartitionOutput(Guid componentID, Guid jobTicketID,
            int partitionCount, int entityCount, 
            string resultFilePath) 
            : base(componentID, jobTicketID)
        {
            this._PartitionCount = partitionCount;
            this._TotalEntityCount = entityCount;
            this._PartitionResultFilePath = resultFilePath;
        }
        #endregion

        public override ResultBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                PartitionOutput output = new PartitionOutput();
                output.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                output.FinishTime = DateTime.Parse(xNode.Attributes.GetNamedItem("FinishTime").Value);

                output.PartitionCount =
                    int.Parse(XmlDataUtil.GetAttributeValue(xNode, "PartitionCount", "0"));
                output.TotalEntityCount = int.Parse(XmlDataUtil.GetAttributeValue(xNode, "TotalEntityCount", "0"));
                output.PartitionResultFilePath = XmlDataUtil.GetAttributeValue(xNode, "ResultFilePath", "");

                return output;
            }
            else
            {
                throw new Exception("Unable to instantiate output: invalid type specified in xml node");
            }
        }

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Output");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FinishTime", this.FinishTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "PartitionCount", this.PartitionCount.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TotalEntityCount", this.TotalEntityCount.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ResultFilePath", this.PartitionResultFilePath);

            return xNode;
        }
    }
}
